// pages/home-music/home-music.js
import { getBannerList } from "../../service/api-music";
import { selectorQuery } from "../../utils/func";
import { hotMusicStore } from "../../store/index";
import "../../utils/lodashFix";
import _ from "lodash";
Page({
  /**
   * 页面的初始数据
   */
  data: {
    bannerList: [],
    hotMusicList: [],
    newMusicList: [],
    originalMusicList: [],
    upMusicList: [],
    topList: [],
    swiperHeight: "0px", //根据图片高度 计算出的swiper高度
  },
  rankingNames: {},

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.fetchBannerList();
    hotMusicStore.dispatch("getHotMusicList");
    hotMusicStore.onState("hotMusicList", (res) => {
      this.setData({ hotMusicList: res.slice(0, 6) });
    });

    hotMusicStore.dispatch("getHotMusicList", 0);
    hotMusicStore.onState("newMusicList", (res) => {
      this.setData({ newMusicList: res.slice(0, 6) });
    });
    hotMusicStore.dispatch("getHotMusicList", 3);
    hotMusicStore.onState("upMusicList", (res) => {
      this.setData({ upMusicList: res.slice(0, 6) });
    });
    hotMusicStore.dispatch("getHotMusicList", 2);
    hotMusicStore.onState("originalMusicList", (res) => {
      this.setData({ originalMusicList: res.slice(0, 6) });
    });
    hotMusicStore.onState("topList", (res) => {
      this.setData({ topList: res });
    });
  },

  async fetchBannerList() {
    try {
      const res = await getBannerList();
      this.setData({ bannerList: res.banners });
    } catch (error) {
      console.log(error.message);
      wx.showToast({
        title: error.message,
        duration: 1500,
      });
    }
  },
  handleSearch() {
    wx.navigateTo({ url: "/pages/search-detail/search-detail" });
  },
  handleSwiperLoaded(e) {
    // 当图片加载完后，获取某个组件的高度。获取图片的高度
    // 需要用节流进行优化  因为调用频率比较高

    _.throttle(
      selectorQuery,
      2000
    )(".swiperImg").then((res) => {
      // console.log(res[0]);
      this.setData({
        swiperHeight: res[0].height + "px",
      });
    });
  },
  clickMore(e) {
    console.log(e.currentTarget.dataset.newmusiclist, 'newMusicList');
    wx.navigateTo({
      url: `/pages/detail-song/detail-song?title=热门歌单&ranking=newMusicList`,
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () { },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () { },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () { },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () { },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () { },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () { },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () { },
});
